<?php

defined('PATH') or exit();

class Helper_FileUpload {

	public static function isLoaded($_file) {
		if (array_key_exists($_file, $_FILES) && $_FILES[$_file]['error'] == 0) {
			return true;
		}
		return false;
	}

	public static function saveUploadedFile($_file, $_location, $_overwrite = false, $_name = null) {
		if (array_key_exists($_file, $_FILES) && $_FILES[$_file]['error'] == 0) {
			if ($_name) {
				$_FILES[$_file]['name'] = $_name;
			}
			if (file_exists($_location . $_FILES[$_file]['name'])) {
				if ($_overwrite) {
					unlink($_location . $_FILES[$_file]['name']);
					move_uploaded_file($_FILES[$_file]['tmp_name'], $_location . $_FILES[$_file]['name']);
				} else {
					return false;
				}
			} else {
				move_uploaded_file($_FILES[$_file]['tmp_name'], $_location . $_FILES[$_file]['name']);
			}

			return true;
		}
		return false;
	}

	public static function getExtUploadedFile($_file) {
		if (array_key_exists($_file, $_FILES) && $_FILES[$_file]['error'] == 0) {
			$fileName = $_FILES[$_file]['name'];
			$ext = substr($fileName, strrpos($fileName, '.') + 1);
			return $ext;
		}
		return false;
	}

	public static function getImageSize($_file) {
		if (array_key_exists($_file, $_FILES) && $_FILES[$_file]['error'] == 0) {
			$size = getimagesize($_FILES[$_file]['tmp_name']);
			return $size; //array (0 => height, 1 => width)
		}
		return false;
	}

}